import Common from '../common'
import region from '../../utils/cityTencent.json'
var Js = Common('message')
export default {
  name: 'message',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: '消息ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'user_no',
          label: '发布用户no',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          formatter: function(val) {
            return val.user.nickname?val.user.nickname+'【'+val.user_no+'】':val.user_no
          },
          width: 150
        },
        {
        	key: "description",
        	label: '类型',
        	application: ['编辑', '添加'],
        	componentName: 'sls-select',
        	listType: 'normal',
        	optionsName: 'labelOptions',
        	defaultProps: {
        		label: 'title',
        		value: 'title',
        	},
        },
        {
        	key: 'title',
        	label: '标题',
        	application: ['编辑', '添加'],
        	componentName: 'sls-input',
        	listType: 'normal',
        	header_search: {
        		componentName: 'sls-input',
        		placeholder: '请输入标题',
        		clearable: true,
        		changeFunc: function(val, self) {
        			if (val) {
        				self.searchItem.title = ['LIKE',['%'+val+'%']];
        			} else {
        				delete self.searchItem.title;
        			};
        		},
        	},
        },
        {
        	key: 'content',
        	label: '内容',
        	application: ['编辑', '添加'],
        	componentName: 'sls-textarea',
        	listType: 'normal',
        	header_search: {
        		componentName: 'sls-input',
        		placeholder: '请输入内容',
        		clearable: true,
        		changeFunc: function(val, self) {
        			if (val) {
        				self.searchItem.content = ['LIKE',['%'+val+'%']];
        			} else {
        				delete self.searchItem.content;
        			};
        		},
        	},
        },
        {
        	key: "description",
        	label: '区域',
        	application: ['编辑', '添加'],
        	componentName: 'sls-cascader',
        	listType: 'normal',
        	optionsName: 'labelOptions',
        	defaultProps: {
            children: 'child',
        		label: 'fullname',
        		value: 'fullname',
            array: true
        	},
          changeFunc: function(val, self){
            self.submitData.region = val.length>0?val.join(','):''
          }
        },
        {
        	key: 'phone',
        	label: '手机号',
        	application: ['编辑', '添加'],
        	componentName: 'sls-input',
        	listType: 'normal',
        	header_search: {
        		componentName: 'sls-input',
        		placeholder: '请输入手机号',
        		clearable: true,
        		changeFunc: function(val, self) {
        			if (val) {
        				self.searchItem.phone = ['LIKE',['%'+val+'%']];
        			} else {
        				delete self.searchItem.phone;
        			};
        		},
        	},
        },
        {
        	key: 'end_time',
        	label: '发布结束时间',
        	application: ['编辑', '添加'],
        	componentName: 'sls-date-picker',
        	listType: 'normal',
          formatter: function(val){
            return val.time1
          }
        },
        {
        	key: 'top_time',
        	label: '置顶结束时间',
        	application: ['编辑', '添加'],
        	componentName: 'sls-date-picker',
        	listType: 'normal',
          formatter: function(val){
            return val.time2
          }
        },
        {
        	key: "behavior",
        	label: '是否置顶',
        	application: ['编辑', '添加'],
        	componentName: 'sls-select',
        	listType: 'normal',
        	optionsName: 'behaviorOptions',
        	defaultProps: {
        		label: 'text',
        		value: 'value',
        	},
          formatter:function(val){
            return val.behavior==1?'是':'否'
          }
        },
        {
          key: 'mainImg',
          label: '图片',
          customSlot: 'mainImg',
          application: ['编辑', '添加'],
          componentName: 'upload',
          listType: 'normal',
          limit: 10,
          dialogStyle: 'width:90%',
        },
        {
          key: "apply_type",
          label: '状态',
          application: [],
          componentName: 'sls-select',
          optionsName: 'typeOptions',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          listType: 'normal',
          width: 100,
          formatter: function(val) {
            return ['待审核','通过','不通过'][val.apply_type-1]
          }
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };

            },
          },
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 200
        },
      ],
      // 按钮配置
      btn_info: [


        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '编辑'
          },
          func: {
            apiName: function(self) {
              return "api_messageUpdate"
            },
            formData: function(self) {
              return self.formData
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                },
                data: self.submitData
              };
              if (self.submitData.parentid && self.submitData.parentid == self.formData.id) {
                self.$$notify('父级ID和子级ID重叠', 'fail');
                return false;
              } else {
                return postData;
              };

            }
          },
        },

        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {

            apiName: function(self) {
              return "api_messageUpdate"
            },

            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }

          },
        },

        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(data) {
            return '添加'
          },
          func: {
            apiName: function(self) {
              return "api_messageAdd"
            },
            formData: function(self) {
              var data = {
                title: '',
                phone: '',
                content: '',
              };
              return data
            },
            postData: function(self) {
              self.submitData.type = 1;
              var postData = {
                data: self.submitData
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          funcType: 'submit',
          position: 'list',
          text: function(data) {
            return '通过'
          },
          styleFunc: function(data) {
            if (data.apply_type === 0) {
              return 'background-color: #029A15;border-color:#029A15'
            } else {
              return 'display: none'
            }
          },
          func: {
            apiName: function(self) {
              return "api_messageUpdate"
            },
            formData: function(self) {
              return self.formData
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                },
                data: {
                  apply_type: 1,
                }
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          funcType: 'submit',
          position: 'list',
          text: function(data) {
            return '不通过'
          },
          styleFunc: function(data) {
            if (data.apply_type == 0) {
              return 'background-color: #df231c;border-color:#df231c'
            } else {
              return 'display: none'
            }
          },
          func: {
            apiName: function(self) {
              return "api_messageUpdate"
            },
            formData: function(self) {
              return self.formData
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                },
                data: {
                  apply_type: -1,
                },
              };
              return postData;
            }
          },
        },
      ],

      searchItem: {
        type: 1
      },
      startSearchItem: {
        type: 1
      },
      optionData: {
        labelOptions: [],
        behaviorOptions: [{
          text: '是',
          value: 1
        }, {
          text: '否',
          value: 0
        }],
        typeOptions: [{
          text: '待审核',
          value: 0
        }, {
          text: '通过',
          value: 1
        }, {
          text: '不通过',
          value: 1
        }],
        regionOptions: []
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        fixSearchItem: {
          status: 1
        },
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      }

    }

  },

  mounted() {
    const self = this;
    self.optionData.regionOptions = region;
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMenuData();
      this.initMainData();
    },


    async initMenuData() {
      const self = this;
      const postData = {};
      postData.searchItem = {
        keywords: '工作招聘',
        menu_id: 4
      };
      postData.token = self.$store.getters.getToken;
      postData.order = {
        listorder: 'asc'
      };
      var res = await self.$$api_articleGet({
        data: postData
      });
      if (res) {
        self.optionData.labelOptions = res.info.data;
      };
    },

    /**
     * 列表主函数
     */
    async initMainData(isNew) {

      const self = this;
      const postData = {};
      if (isNew) {
        self.paginate.currentPage = 1;
      };
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      postData.getAfter = {
        user: {
          tableName: 'User',
          middleKey: 'user_no',
          key: 'user_no',
          condition: '=',
          info: ['nickname', 'headImgUrl', 'login_name']
        }
      }
      var res = await self.$$api_messageGet({
        data: postData
      });
      // for(var i = 0;i < res.info.data.length;i++){
      //   res.info.data[i].region = res.info.data[i].region?res.info.data[i].region.split(','):[];
      //   res.info.data[i].time1 = self.$$timeto(res.info.data[i].end_time*1000,'ymd-hms','-')
      //   res.info.data[i].time2 = self.$$timeto(res.info.data[i].top_time*1000,'ymd-hms','-')
      // }
      self.mainData = res.info.data;
      self.paginate.count = res.info.total;

    }

  },


}
